package com.xiaomaoguai.jdk.thread;

import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Description :
 * @FileName: TestCountDownLatch.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年6月25日 下午4:52:08
 * @Version:V1.00
 */
public class TestCountDownLatch {

	private final static Integer THREAD_NUMBER = 20;

	private static final ExecutorService executorService = Executors.newFixedThreadPool(THREAD_NUMBER);

	private static CountDownLatch doneSignal;

	private final static ConcurrentMap<String, Integer> counterMap = new ConcurrentHashMap<String, Integer>();

	private static void prepareData() {
		counterMap.put("1", 1);
		counterMap.put("2", 2);
		counterMap.put("3", 3);
		counterMap.put("4", 4);
		counterMap.put("5", 5);
		counterMap.put("6", 6);
		counterMap.put("7", 7);
		counterMap.put("8", 8);
		counterMap.put("9", 9);
		counterMap.put("10", 10);
	}

	public static void main(String[] args) {
		prepareData();

		Set<Entry<String, Integer>> entrySet = counterMap.entrySet();
		doneSignal = new CountDownLatch(entrySet.size());

		for (final Entry<String, Integer> entry : entrySet) {
			executorService.execute(new Runnable() {

				@Override
				public void run() {
					System.out.println("-----------------------------------------");
					System.out.println(Thread.currentThread().getName());
					System.out.print(entry.getKey() + "<--->");
					System.out.println(entry.getValue());
					doneSignal.countDown();
				}
			});
		}
		try {
			doneSignal.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("更新首页数据。。");
	}
}
